home *** CD-ROM | disk | FTP | other *** search
- ===========================================================================
- BBS: The Abacus * HST/DS * Potterville, MI
- Date: 05-14-93 (02:41) Number: 42
- From: SEAN MUNSON Refer#: NONE
- To: ALL Recvd: NO
- Subj: blah Conf: (36) C Language
- ---------------------------------------------------------------------------
- Here's a Program to Change from Arabic to Roman numerals and back.
- #include<stdio.h>;
- #include<string.h>;
- /*
- this Program is hereby donated to the public domain by the author,
- Sean Munson. Enjoy it! no Promises or elsewise.
- */
- char * arabictoroman(int x);
- int *romantoarabic(char *r);
- main(){
- char *romannum;
- int arabicnum;
- printf("\n enter a roman numeral here:");
- gets(romannum);
- printf(" %s = %d \n",arabicnum,romantoarabic(romannum));}
- char * arabictoroman(int x){
- char tval[50]={" "};
- int i=0;
- while (x>1000){x-=1000; tval[i++]='m'; }
- while (x>900) {x-=900; tval[i++]='c'; tval[i++]='m';}
- while (x>500) {x-=500; tval[i++]='d'; }
- while (x>400) {x-=400; tval[i++]='c'; tval[i++]='d';}
- while (x>100) {x-=100; tval[i++]='c'; }
- while (x>90) {x-=90; tval[i++]='x'; tval[i++]='c';}
- while (x>50) {x-=50; tval[i++]='l'; }
- while (x>40) {x-=40; tval[i++]='x'; tval[i++]='x';}
- while (x>10) {x-=10; tval[i++]='x'; }
- while (x>9) {x-=9; tval[i++]='i'; tval[i++]='x';}
- while (x>5) {x-=5; tval[i++]='v'; }
- while (x>4) {x-=4; tval[i++]='i'; tval[i++]='v';}
- while (x>=1) {x--; tval[i++]='i'; }
- tval[i++]=0;
- return tval;
- }
- int romantoarabic(char *r){
- int sum=0;
- int i=0;
- strlwr(r);
- for (i=0;i<strlen(r);i++){
- switch(r[i]){
- case 'i': if (r[i+1]=='v'){sum+=4;i++;break;}
- else sum++;
- break;
- case 'v': if (r[i+1]=='x'){sum+=9;i++;break;}
- else sum+=5;
- break;
- case 'x': if (r[i+1]=='c'){sum+=90;i++;break;}
- else if (r[i+1]=='l'){sum+=40;i++;break;}
- else sum+=10;
- break;
- case 'l' :sum+=50;
- break;
- case 'c' :if (r[i+1]=='m'){sum+=900;i++;break;}
- else if (r[i+1]=='d'){sum+=400;i++;break;}
- else sum+=100;
- break;
- case 'd' :sum+=500;
- break;
- case 'm' :sum+=1000;
- break;
- }
- }
- return sum;
- }
-
- --- Maximus/2 2.01wb
- * Origin: The Excelsior (1:141/222)
- SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
- SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 261/1023
- SEEN-BY: 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
-